package Box2D.Dynamics
{
	import Box2D.Common.Math.b2Transform;
	import Box2D.Common.Math.b2Vec2;
	import Box2D.Common.b2Color;
	import Box2D.Common.b2Wrapper;
	
	import flash.display.Sprite;
	
	public class b2DebugDraw extends b2Wrapper
	{
		public static const e_shapeBit:uint 	= 0x0001;
		public static const e_jointBit:uint 	= 0x0002;
		public static const e_aabbBit:uint 	= 0x0004;
		public static const e_pairBit:uint 	= 0x0008;
		public static const e_centerOfMassBit:uint 	= 0x0010;
		
		public var sprite:Sprite;
		public var lineThickness:Number = 1.0;
		public var alpha:Number = 1.0;
		public var fillAlpha:Number = 1.0;
		public var drawScale:Number = 1.0;
		public var xformScale:Number = 1.0;
		public function b2DebugDraw()
		{
			super();
			_wrapper = callDirect("b2Draw_new",this);
		}		
		public function SetFlags(flags:uint):void{
			call('b2Draw_SetFlags',flags);
		}
		public function GetFlags():uint{
			return call('b2Draw_GetFlags');			
		}
		public function AppendFlags(flags:uint):void{
			call('b2Draw_AppendFlags',flags);			
		}
		public function ClearFlags(flags:uint):void{
			call('b2Draw_ClearFlags',flags);			
		}		
		public function cDrawPolygon(verticesPtr:Object,vertexCount:int,colorPtr:Object):void{
			var vertices:Vector.<b2Vec2> = b2Vec2.getVector(verticesPtr,vertexCount);
			var color:b2Color = new b2Color(colorPtr);
			DrawPolygon(vertices,vertexCount,color);
		}
		public function cDrawSolidPolygon(verticesPtr:Object,vertexCount:int,colorPtr:Object):void{
			var vertices:Vector.<b2Vec2> = b2Vec2.getVector(verticesPtr,vertexCount);
			var color:b2Color = new b2Color(colorPtr);
			DrawSolidPolygon(vertices,vertexCount,color);
		}
		public function cDrawCircle(centerPtr:Object, radius:Number, colorPtr:Object) : void{
			var center:b2Vec2 = new b2Vec2(0,0,centerPtr);
			var color:b2Color = new b2Color(colorPtr);		
			DrawCircle(center,radius,color);
		}
		public function cDrawSolidCircle(centerPtr:Object, radius:Number, axisPtr:Object, colorPtr:Object) : void{
			var center:b2Vec2 = new b2Vec2(0,0,centerPtr);
			var axis:b2Vec2 = new b2Vec2(0,0,axisPtr);
			var color:b2Color = new b2Color(colorPtr);
			DrawSolidCircle(center,radius,axis,color);
		}
		public function cDrawSegment(p1Ptr:Object, p2Ptr:Object, colorPtr:Object) : void{
			var p1:b2Vec2 = new b2Vec2(0,0,p1Ptr);
			var p2:b2Vec2 = new b2Vec2(0,0,p2Ptr);
			var color:b2Color = new b2Color(colorPtr);
			DrawSegment(p1,p2,color);
		}
		public function cDrawTransform(xfPtr:Object) : void{
			var xf:b2Transform = new b2Transform(xfPtr);
			DrawTransform(xf);
		}

		public virtual function DrawPolygon(vertices:Vector.<b2Vec2>,vertexCount:int,color:b2Color):void{
			sprite.graphics.lineStyle(lineThickness, color.color, alpha);
			sprite.graphics.moveTo(vertices[0].x * drawScale, vertices[0].y * drawScale);
			for (var i:int = 1; i < vertexCount; i++){
				sprite.graphics.lineTo(vertices[i].x * drawScale, vertices[i].y * drawScale);
			}
			sprite.graphics.lineTo(vertices[0].x * drawScale, vertices[0].y * drawScale);			
		}		
		public  virtual function DrawSolidPolygon(vertices:Vector.<b2Vec2>,vertexCount:int,color:b2Color):void{			
			sprite.graphics.lineStyle(lineThickness, color.color, alpha);
			sprite.graphics.moveTo(vertices[0].x * drawScale, vertices[0].y * drawScale);
			sprite.graphics.beginFill(color.color, fillAlpha);
			for (var i:int = 1; i < vertexCount; i++)
			{
				sprite.graphics.lineTo(vertices[i].x * drawScale, vertices[i].y * drawScale);
			}
			sprite.graphics.lineTo(vertices[0].x * drawScale, vertices[0].y * drawScale);
			sprite.graphics.endFill();			
		}		
		public virtual function DrawCircle(center:b2Vec2, radius:Number, color:b2Color) : void{			
			sprite.graphics.lineStyle(lineThickness, color.color, alpha);
			sprite.graphics.drawCircle(center.x * drawScale, center.y * drawScale, radius * drawScale);			
		}
		public virtual function DrawSolidCircle(center:b2Vec2, radius:Number, axis:b2Vec2, color:b2Color) : void{			
			sprite.graphics.lineStyle(lineThickness, color.color, alpha);
			sprite.graphics.moveTo(0,0);
			sprite.graphics.beginFill(color.color, fillAlpha);
			sprite.graphics.drawCircle(center.x * drawScale, center.y * drawScale, radius * drawScale);
			sprite.graphics.endFill();
			sprite.graphics.moveTo(center.x * drawScale, center.y * drawScale);
			sprite.graphics.lineTo((center.x + axis.x*radius) * drawScale, (center.y + axis.y*radius) * drawScale);			
		}
		public virtual function DrawSegment(p1:b2Vec2, p2:b2Vec2, color:b2Color) : void{			
			sprite.graphics.lineStyle(lineThickness, color.color, alpha);
			sprite.graphics.moveTo(p1.x * drawScale, p1.y * drawScale);
			sprite.graphics.lineTo(p2.x * drawScale, p2.y * drawScale);			
		}		
		public virtual function DrawTransform(xf:b2Transform) : void{			
			sprite.graphics.lineStyle(lineThickness, 0xff0000, alpha);
			sprite.graphics.moveTo(xf.position.x * drawScale, xf.position.y * drawScale);
			sprite.graphics.lineTo((xf.position.x + xformScale*xf.R.col1.x) * drawScale, (xf.position.y + xformScale*xf.R.col1.y) * drawScale);
			
			sprite.graphics.lineStyle(lineThickness, 0x00ff00, alpha);
			sprite.graphics.moveTo(xf.position.x * drawScale, xf.position.y * drawScale);
			sprite.graphics.lineTo((xf.position.x + xformScale*xf.R.col2.x) * drawScale, (xf.position.y + xformScale*xf.R.col2.y) * drawScale);
			
		}
	}
}